﻿package {
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.system.ApplicationDomain;
	
	/**
	 * ...
	 * @author kot
	 * 
	 */
	public class  Pawn extends Figure {
		
		private var twoMoved = false;
		//TODO взятие на проходе
		override public function toString():String {
			return "Pawn";
		}
		
		public function canPawnEat(x : int, y : int) {
			if (!insideBoard(x, y)) {
				return false;
			}
			if (Board(parent).getCell(x, y).isEmpty()) {
				return false;
			}
			return (Board(parent).getCell(x, y).getFigure().getFigureColor() != figureColor);
		}
		
		override public function getAvailableMoves(): int {
			resetMoves();
			if (figureColor == 2) {//для белых
				if (figureY == 1 && Board(parent).getCell(figureX, figureY + 2).isEmpty()
					&& Board(parent).getCell(figureX, figureY + 1).isEmpty()) {
					moves.push(new Point(figureX, figureY + 2));
				}
				if (Board(parent).getCell(figureX, figureY + 1).isEmpty()) {
					moves.push(new Point(figureX, figureY + 1));
				}
				for (var i : int = -1; i < 2; i++) {
					if (i == 0)
						continue;
					if (canPawnEat(figureX + i, figureY + 1)) {
						moves.push(new Point(figureX + i, figureY + 1));
					}
				}
			} else {//для черных
				if (figureY == 6 && Board(parent).getCell(figureX, figureY - 2).isEmpty()
					&& Board(parent).getCell(figureX, figureY - 1).isEmpty()) {
					moves.push(new Point(figureX, figureY - 2));
				}
				if (Board(parent).getCell(figureX, figureY - 1).isEmpty()) {
					moves.push(new Point(figureX, figureY - 1));
				}
				for (var j : int = -1; j < 2; j++) {
					if (j == 0)
						continue;
					if (canPawnEat(figureX + j, figureY - 1)) {
						moves.push(new Point(figureX + j, figureY - 1));
					}
				}
			}
			return moves.length;
		}
		
		override public function canMove(x : int, y : int) : Boolean {
			if (figureX == x && figureY == y)
				return true;
			if (figureColor == BLACK) {
				if (figureY == y + 1 && (figureX - 1 == x || figureX + 1 == x)) {
					return true;
				} else {
					return false;
				}
			} else {
				if (figureY == y - 1 && (figureX - 1 == x || figureX + 1 == x )) {
					return true;
				} else {
					return false;
				}
			}
		}
		
		public function updatePawn(cellToMove : Cell) : void {
			var fig : Figure;
			if (figureColor == WHITE && cellToMove.nameY == 7){ 
				var QueenW : Class = ApplicationDomain.currentDomain.getDefinition("QueenWhite") as Class;
				fig = new QueenW();
				Board(parent).getCell(figureX, figureY).removeFigure();
				fig.setFigureColor(WHITE);
				Board(parent).addChild(fig);
				cellToMove.addFigure(fig);
				this.x = -100;
				this.y = -100;
			} else if (figureColor == BLACK && cellToMove.nameY == 0){ 
				var QueenB : Class = ApplicationDomain.currentDomain.getDefinition("QueenBlack") as Class;
				fig = new QueenB();
				fig.setFigureColor(BLACK);
				Board(parent).getCell(figureX, figureY).removeFigure();
				Board(parent).addChild(fig);
				cellToMove.addFigure(fig);
				this.x = -100;
				this.y = -100;
			}
		}
	}
	
}